import apiClient from '../../apiClient';

import { ListData, Menu, PageForm, Rule, UserInfo, UserToken } from '#/entity';

export enum UserApi {
  SignIn = '/user/code/sign_in',
  SignUp = '/auth/signup',
  Logout = '/user/sign_out',
  MenusPermissions = '/security/has-rule/menus',
  HasRules = '/session/has_rules',
  User = '/user',
  Session = '/session',
  List = '/users',
  essential = '/users/essential',
}

export interface UserQueryForm extends PageForm {
  name?: string;
}

export interface UpdateUserReq {
  name?: string;
  email?: string;
  mobile?: string;
  gender?: string;
  code?: string;
}

export interface UserListResponse {
  success: {
    count: number;
    data: UserInfo[];
  };
}

export interface CreateUserReq {
  name: string;
  email: string;
  mobile: string;
  gender: string;
  code: string;
}

export interface SignInReq {
  code: string;
  password: string;
  app: string;
  remember: boolean;
  captcha: string;
  uuid: string;
}

export interface SignUpReq extends SignInReq {
  email: string;
}

export type SignInRes = UserToken & { user: UserInfo };

const signin = (data: SignInReq) => apiClient.post<UserInfo>({ url: UserApi.SignIn, params: data });

const signup = (data: SignUpReq) => apiClient.post<UserInfo>({ url: UserApi.SignUp, data });

const logout = () => apiClient.post({ url: UserApi.Logout });

const menuPermissions = () => apiClient.get<Menu[]>({ url: UserApi.MenusPermissions });

const findById = (id: string) => apiClient.get<UserInfo[]>({ url: `${UserApi.User}/${id}` });

const getUserList = (data: any) =>
  apiClient.get<ListData<UserInfo>>({ url: UserApi.List, params: data });

const getDepartmentUserList = (data: any) =>
  apiClient.get<ListData<UserInfo>>({
    url: `/departments_users/${data.departmentId}/users`,
    params: data,
  });

const updateUser = (id: number, data: UpdateUserReq) =>
  apiClient.post<number>({ url: `/user/${id}`, params: data });

const createUser = (data: CreateUserReq) =>
  apiClient.post<UserInfo>({ url: '/user', params: data });

const enableUsers = (data: any) => apiClient.post<number>({ url: '/user/enable', data });

const disableUsers = (data: any) => apiClient.post<number>({ url: '/user/disabled', data });

const getUserById = (id: number) => apiClient.get<UserInfo>({ url: `/user/${id}` });

const resetPassword = (id: string) => apiClient.post<number>({ url: `/user/reset/${id}/password` });

const hasRules = () => apiClient.get<Rule[]>({ url: UserApi.HasRules });

const session = () => apiClient.get<UserInfo>({ url: UserApi.Session });

// 揽收员列表
// /wl_users
const getWLUser = (data: any) => apiClient.get<any>({ url: '/wl_users', params: data });

const essential = (data?: any) => apiClient.get({ url: UserApi.essential, params: data });

export default {
  hasRules,
  signin,
  signup,
  findById,
  menuPermissions,
  logout,
  getUserList,
  updateUser,
  createUser,
  enableUsers,
  disableUsers,
  getUserById,
  resetPassword,
  getDepartmentUserList,
  getWLUser,
  session,
  essential,
};
